/**
* @author  rockydai(dzjdai@gmail.com)
* @version 创建时间：2008-11-29 下午04:32:45
* 类说明：业务逻辑相关的Filter
*/
package org.nielin.bestwaper.core.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 1.  通知Player在线；
 * 2.  将原子动作检查设置到request中，便于后面的操作调用
 * 3.  要求玩家处理目前待确认的消息
 */
public class LogicFilter implements Filter {

    /* (non-Javadoc)
     * @see javax.servlet.Filter#destroy()
     */
    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

    /* (non-Javadoc)
     * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
     */
    @Override

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException
    {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        
        String queryString = req.getQueryString();  //访问的queryString
        String methodString = req.getMethod();      //方法,一般为POST或者GET
        String URI = req.getRequestURI();           //要访问的URI
        
        AtomicAction atomicAction = null;
        
        String profileMsg = "LogicFilter.doFilter, uri[" + URI + "]queryString[" + 
            queryString + "]method["+methodString+"]";
        UtilTimerStack.push(profileMsg);
        try {
            SessionUserBean user = (SessionUserBean)request.getAttribute( "userBean" );
            
            if ( user != null ) {
                String playerID = user.getPlayerID();
                if ( !playerID.equals("") ) {

                    Player player = ObjFactoryBuilder.getDefaultObjFactory().get( playerID );
                    
                    String pageId = req.getParameter(Dispatcher.PARAM_PAGE_ID);             // dispatch之前的id
                    
                    // 1.玩家在线通知
                    player.onlineNotify();

                    // 2.将原子动作检查设置到request中，便于后面的操作调用
                    atomicAction = player.getAtomicAction();
                    if( atomicAction != null && !atomicAction.isFinished() ) {
                        req.setAttribute( "atomicAction", atomicAction );
                    }
                    else {
                        req.setAttribute( "atomicAction", null );
                    }
                    
                    // 3.要求玩家处理目前待确认的消息
                    if ( !pageId.equals(AffirmMessage.DEAL_AFFIRMMSG_PAGE_ID) ) {
                        SimpleObj<AffirmMessage> affirmMsgS = player.getOneAffirmMessage();
                        if ( affirmMsgS != null ) {
                        	AffirmMessage affirmMsg = affirmMsgS.enrich();
                        	if ( affirmMsg.isAlive() ) {
	                            if ( affirmMsg.getAnswerEns().size() == 0 ) {
	                                // 无选项的消息
	                                player.removeAndGeOneAffirmMessage();
	                            }
	                            
	                            affirmMsg.displayOnAllPage(req, res);
                        	}
                        	else {
                        		// 消息已经超过其生命周期，不显示，直接删除
                                player.removeAndGeOneAffirmMessage();
                        	}
                        }                     
                    }

                    player.save();
                }
            }
            
            UtilTimerStack.push("chain.doFilter");
            chain.doFilter(request, response);          
            UtilTimerStack.pop("chain.doFilter");
            
        }    
        catch (Exception e)
        {
            ErrorPageBean errorpage = new ErrorPageBean("内部错误");
            MyLogger.getNormal().error("[" + errorpage.getErrno() + "]LogicFilter process error:queryString="
                    + req.getQueryString(), e);
            Dispatcher.toErrorPage(req, res, errorpage);
        }
        finally {
          UtilTimerStack.pop(profileMsg); //this needs to be the same text as above
        }
    }
    
    /* (non-Javadoc)
     * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
     */
    @Override
    public void init(FilterConfig arg0) throws ServletException {
        // TODO Auto-generated method stub

    }

}
 